package com.example.shengyin.controller;

import com.example.shengyin.entity.*;
import com.example.shengyin.service.ProfessorService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/professor")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("专家")
public class ProfessorController {
    @Autowired
    private ProfessorService service;

    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("预约列表")
    @GetMapping("/search")
    public Result event(@ApiParam(name = "pname",value = "预约人名") @RequestParam(required = false) String pname,
                        @ApiParam(name = "ptype" ,value = "专家类型") @RequestParam(required = false) String ptype,
                        @ApiParam(name = "pos" ,value = "区域") @RequestParam(required = false) String pos,
                        @RequestParam(name = "page",defaultValue = "1") int page,
                        @RequestParam(name = "limit",defaultValue = "10")int limit) {
        Page p = service.selectList(pname,ptype,pos, page, limit);
        int count = service.fand(pname,ptype,pos);
        if(count==0||p==null)
            return Result.error("没有数据");
        return Result.success(p);
    }

    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Professor select = service.selectById(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("增加预约列表")
    @PostMapping("/add")
    public Result insert(@Valid Professor professor, HttpServletRequest request) {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        professor.setPoster(admin.getUname());
        int insert = service.add(professor);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("修改预约列表")
    @PostMapping("/update")
    public Result update(Professor professor)
    {
        int insert = service.update(professor);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id)
    {
        int delete = service.delById(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.delByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @ApiOperation("修改状态按钮")
    @GetMapping("/isvalue/{id}/{isvalue}")
    public Result updateIsvalid(@PathVariable(value = "id")Long id, @PathVariable(value = "isvalue")Boolean isvalue,HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }

        int symtype = service.updateIsvalid(isvalue, id);
        if (symtype>0) {
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }

    }
}
